SPB Git

Repository Public

route.ts 4.7 KB 100 lines typescript

1/**2 * =============================================================================3 *                  SP BOUCHER | ACADEMIC RESEARCH4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * PROJECT METADATA8 * -----------------------------------------------------------------------------9 * Project          : UQO Éval10 * File             : src/app/api/listings/[uid]/ai-cost-analysis/route.ts11 * Module           : app12 * Repository       : https://git.spboucher.ai/uqo-eval13 * Project Website  : https://www.uqo-eval.app14 * Created          : 2026-10-0215 * Last Modified    : 2026-10-0216 *17 * =============================================================================18 * SOFTWARE MAINTAINER & AFFILIATION19 * =============================================================================20 *21 * Maintainer       : Simon-Pierre Boucher22 * Position         : Professor of Finance23 * Institution      : Université du Québec en Outaouais (UQO)24 * Department       : Département des sciences administratives25 * Website          : https://spboucher.ai26 * Git              : https://git.spboucher.ai27 *28 * Research Contact : research@spboucher.ai29 * Direct Contact   : simon@spboucher.ai30 * Security/Admin   : admin@spboucher.ai31 *32 * The affiliation is descriptive. It is not an institutional endorsement,33 * a license grant or a determination of ownership.34 *35 * =============================================================================36 * COPYRIGHT, AUTHORSHIP & DATA37 * =============================================================================38 *39 * Copyright (c) the author(s) of this work.40 * Ownership is not asserted by this notice; see the repository records.41 *42 * This header identifies the maintainer of the software. Scientific43 * authorship of any associated paper is stated in the paper itself and44 * is not changed by this notice. Datasets and third-party materials45 * remain the property of their respective owners and licensors.46 *47 * This file is governed by the applicable project license, copyright48 * notices, institutional agreements and relevant law.49 *50 * =============================================================================51 * LICENSE & MAINTENANCE52 * =============================================================================53 *54 * License          : None specified (no license file in this repository)55 * Classification   : Public56 *57 * =============================================================================58 * END OF SP BOUCHER SOURCE HEADER59 * =============================================================================60 */6162// UQO Éval — analyse multimodale d'une annonce : démarrage (POST, sur demande seulement) et suivi/lecture (GET).63import { NextRequest, NextResponse } from "next/server";64import { AnalysisError, getById, getLatest, startAnalysis } from "@/lib/cost/ai/service";65import { clientKey, rateLimit } from "@/lib/cost/cache";6667export const runtime = "nodejs";68export const maxDuration = 60;6970export async function POST(req: NextRequest, { params }: { params: Promise<{ uid: string }> }) {71  const { uid } = await params;72  if (!rateLimit(`ai:${clientKey(req)}`, 3, 10 * 60_000)) return NextResponse.json({ error: "Trop d'analyses lancées — réessayez dans quelques minutes.", code: "rate_limited" }, { status: 429 });73  try {74    const r = await startAnalysis(decodeURIComponent(uid), { force: false });75    return NextResponse.json(r, { status: r.reused ? 200 : 202 });76  } catch (e) {77    return errorResponse(e);78  }79}8081export async function GET(req: NextRequest, { params }: { params: Promise<{ uid: string }> }) {82  const { uid } = await params;83  const id = req.nextUrl.searchParams.get("id");84  try {85    const v = id ? getById(id) : getLatest(decodeURIComponent(uid));86    if (!v) return NextResponse.json({ status: "none" }, { headers: { "Cache-Control": "no-store" } });87    if (v.listingUid !== decodeURIComponent(uid)) return NextResponse.json({ error: "analyse d'une autre annonce" }, { status: 400 });88    // en cours : réponse légère pour le polling89    if (v.status !== "completed" && v.status !== "failed") return NextResponse.json({ id: v.id, status: v.status, stage: v.stage, version: v.version, imageCount: v.imageCount, createdAt: v.createdAt }, { headers: { "Cache-Control": "no-store" } });90    return NextResponse.json(v, { headers: { "Cache-Control": "no-store" } });91  } catch (e) {92    return errorResponse(e);93  }94}9596function errorResponse(e: unknown) {97  if (e instanceof AnalysisError) return NextResponse.json({ error: e.message, code: e.code }, { status: e.status });98  return NextResponse.json({ error: (e as Error).message ?? "erreur", code: "error" }, { status: 500 });99}100
Files main
Menu

Theme